<template>
  <div class="container">
    <div class="dropdown">
      <el-dropdown @command="handleCommand">
        <el-button type="primary">
          更多菜单<i class="el-icon-arrow-down el-icon--right"></i>
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-for="(item, index) in category" :key="index" :command="item.id">{{ item.categoryName
          }}</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <div class="content">
      <div class="hot">
        <div class="titlee">
          <el-tag style="width: 100%; font-size: 20px;">{{ categoryName }}</el-tag>
          <div class="boxx">
            <ul>
              <li v-for="(item, index) in records" :key="index">
                <div class="image">
                  <img :src="item.imgUrl" alt="">
                  <div class="detailTips">
                    <span style="color: #fff;margin-left: 20px;" @click="goToDetail(item.id)">查看详情</span>
                    <span class="buyButton" @click="BuyHandle(item.id)">立即购买</span>
                  </div>
                </div>
                <div class="cakeName">{{ item.cakeName }}</div>
                <div class="cakePrice">￥{{ item.price / 100 }}</div>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
    <div class="bottom">
      <el-pagination background layout="prev, pager, next" @current-change="currentChange" :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getCategory, getCakeById ,addCake} from '@/api/index';
export default {
  data() {
    return {
      category: [],
      pageNum: 1,
      pageSize: 8,
      records: [],
      categoryName: "全部商品",
      total:0,
      command:'',
    }
  },
  methods: {
    BuyHandle(id) {
      let param = {
        cakeId:id,
        count:1
      }
      addCake(param).then(res => {
        this.$message({
          type:'success',
          message:'添加成功！'
        }).catch(err =>{
          this.$message({
            type:'error',
            message:'添加失败，请检查网络'
          })
        })
      })
    },
    goToDetail(id) {
        this.$router.push({
          path:'/detail',
          query:{
            id
          }
        })
      },
    handleCommand(command) {
      if (command) {
        this.command = command
        getCakeById(this.pageNum, this.pageSize, command).then(res => {
          this.records = res.data.records
          this.categoryName = this.records[0].categoryName
          
        })
      } else (
        getCakeById(this.pageNum, this.pageSize, '').then(res => {
          this.records = res.data.records
          this.total = res.data.total
        })
      )

    },
    getCategoryData() {
      getCategory().then((res) => {
        this.category = res.data.records
      })
    },
    currentChange(pageNum){
      getCakeById(pageNum, this.pageSize, this.command).then(res => {
          this.records = res.data.records
        })
    }
  },
  mounted() {
    this.getCategoryData()
    this.handleCommand()
  }
}
</script>

<style scoped lang="less">
.container {
  width: 1200px;
  .hot {
  margin-top: 30px;

  .image img {
    width: 100%;
    height: 280px;
  }

  .titlee {
    margin-bottom: 20px;
  }

  .boxx ul {
    width: 100%;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-between;

    li {
      &:hover {
        border: 2px solid #409eff;
      }

      border: 2px solid transparent;
      width: 23%;
      // margin: 1px;
      margin-top: 10px;
      margin-left: 3px;
      padding: 5px;
      overflow: hidden;

      .image {
        cursor: pointer;
        position: relative;

        .detailTips {
          // display: none;
          width: 100%;
          height: 50px;
          position: absolute;
          bottom: 50%;
          background: rgba(0, 0, 0, 0.5);
          line-height: 50px;
          font-size: 14px;
          margin-left: 1000px;
          transition: all .25s;

          .buyButton {
            margin-top: 10px;
            margin-right: 20px;
            float: right;
            display: flex;
            justify-content: center;
            align-items: center;
            color: #fff;
            border: 1px solid #fff;
            width: 100px;
            height: 30px;
          }
        }
      }

      .image:hover .detailTips {
        // display: block;
        margin-left: 0;
        cursor: pointer;
      }

      .cakeName {
        font-size: 16px;
        font-weight: bold;
        margin: 5px 0;
      }

      .cakePrice {
        color: red;
        font-size: 32px;
      }
    }
  }
  
}
.bottom{
    width: 100%;
    display: flex;
    justify-content: center;
    margin-bottom: 10px;
  }
}
</style>